
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, 2012 and Azure
-- --------------------------------------------------
-- Date Created: 12/16/2013 01:55:59
-- Generated from EDMX file: C:\work_outside\loancalculator\LoanBusiness\Model\LoanModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [FinancialCal];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------


-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------


-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Loans'
CREATE TABLE [dbo].[Loans] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [BankName] nvarchar(max)  NOT NULL,
    [Principle] decimal(18,0)  NOT NULL,
    [Installment] decimal(18,0)  NOT NULL,
    [Interest] float  NOT NULL,
    [StartDate] datetime  NOT NULL,
    [UserId] int  NOT NULL
);
GO

-- Creating table 'Conditions'
CREATE TABLE [dbo].[Conditions] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [DateFrom] datetime  NOT NULL,
    [DateTo] datetime  NOT NULL,
    [Interest] nvarchar(max)  NOT NULL,
    [LoanId] int  NOT NULL
);
GO

-- Creating table 'Users'
CREATE TABLE [dbo].[Users] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [FBID] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Banks'
CREATE TABLE [dbo].[Banks] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [MLR] nvarchar(max)  NOT NULL,
    [MRR] float  NOT NULL,
    [MOR] float  NOT NULL
);
GO

-- Creating table 'ChangedInstallments'
CREATE TABLE [dbo].[ChangedInstallments] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Installment] decimal(18,0)  NOT NULL,
    [Period] int  NOT NULL,
    [LoanId] int  NOT NULL
);
GO

-- Creating table 'Promotions'
CREATE TABLE [dbo].[Promotions] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Interest] nvarchar(max)  NOT NULL,
    [BankId] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'Loans'
ALTER TABLE [dbo].[Loans]
ADD CONSTRAINT [PK_Loans]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Conditions'
ALTER TABLE [dbo].[Conditions]
ADD CONSTRAINT [PK_Conditions]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [PK_Users]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Banks'
ALTER TABLE [dbo].[Banks]
ADD CONSTRAINT [PK_Banks]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'ChangedInstallments'
ALTER TABLE [dbo].[ChangedInstallments]
ADD CONSTRAINT [PK_ChangedInstallments]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Promotions'
ALTER TABLE [dbo].[Promotions]
ADD CONSTRAINT [PK_Promotions]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [UserId] in table 'Loans'
ALTER TABLE [dbo].[Loans]
ADD CONSTRAINT [FK_UserLoan]
    FOREIGN KEY ([UserId])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserLoan'
CREATE INDEX [IX_FK_UserLoan]
ON [dbo].[Loans]
    ([UserId]);
GO

-- Creating foreign key on [LoanId] in table 'Conditions'
ALTER TABLE [dbo].[Conditions]
ADD CONSTRAINT [FK_LoanCondition]
    FOREIGN KEY ([LoanId])
    REFERENCES [dbo].[Loans]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_LoanCondition'
CREATE INDEX [IX_FK_LoanCondition]
ON [dbo].[Conditions]
    ([LoanId]);
GO

-- Creating foreign key on [LoanId] in table 'ChangedInstallments'
ALTER TABLE [dbo].[ChangedInstallments]
ADD CONSTRAINT [FK_LoanChangedInstallment]
    FOREIGN KEY ([LoanId])
    REFERENCES [dbo].[Loans]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_LoanChangedInstallment'
CREATE INDEX [IX_FK_LoanChangedInstallment]
ON [dbo].[ChangedInstallments]
    ([LoanId]);
GO

-- Creating foreign key on [BankId] in table 'Promotions'
ALTER TABLE [dbo].[Promotions]
ADD CONSTRAINT [FK_BankPromotion]
    FOREIGN KEY ([BankId])
    REFERENCES [dbo].[Banks]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BankPromotion'
CREATE INDEX [IX_FK_BankPromotion]
ON [dbo].[Promotions]
    ([BankId]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------